(function ($) {
	var inputFiles = []
	var pullObj
	var sourceData=[];
	var preData
	var page = 0,Limit = 20;
	var pulling = false;
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//未回答列表
	function getAskData(){
		if (pulling) {
			pullObj.endPullUpToRefresh()
			return;
		}
		pulling = true
		var parr = preData['rw']+'^A,N,Y^N,Y'; 
		var pdict = {'parr':parr,'input':$('#search')[0].value,'start':page,'limit':Limit}
		//console.log(JSON.stringify(pdict))
		var paramDict = {'method':'web.INMTeaComm:LoadVideoInfoList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			pulling = false
			if (result['msgcode'] != '1') {
				mui.toast(result.msg)
				pullObj.endPullUpToRefresh();
				return
			}
			var dataArr = result.data
			if (page == 0) {
				sourceData = dataArr
				$('#UL')[0].innerHTML = ''
			}else{
				sourceData = sourceData.concat(dataArr)
			}
			updateUI(dataArr)
			if (dataArr.length < Limit) {
				pullObj.endPullUpToRefresh(true);
			}else{
				pullObj.endPullUpToRefresh();
			}
			page += dataArr.length;
		}, function(errorStr) {
			pullObj.endPullUpToRefresh();
			pulling = false
		});
	}

	function updateUI(dataArr){
		var UL = document.getElementById("UL");
		var oldCount = UL.querySelectorAll('li').length
		for(var i = 0; i < dataArr.length; i++){
			var index = oldCount+i;
			var dict = dataArr[i]
			var statusDesc = '未作答'
			if (dict['AnswerStatus'] == 'Y') { //未回答 N保存 Y提交
				ansHtml = '【回复】'+dict['Answer']
				statusDesc = '已提交'
			}else{
				if (dict['AnswerStatus'] == 'N') {
					statusDesc = '已保存'
				}
				ansHtml = '<div class="mui-input-row">'+
					'<input type="text" id="input-'+index+'" value="'+dict['Answer']+'" class="mui-input-clear" placeholder="请输入您的答案"></div>'+
					'<div class="btnArea">'+
						'<div id="'+index+'" class="mui-btn mui-btn-primary publishBtn" data-id="Y">提交</div>'+
						'<div id="'+index+'" class="mui-btn mui-btn-primary saveBtn" data-id="N">保存</div>'+
					'</div>'
			}
			var html = '<div class="queTitle"><div class="queContent">'+(index+1)+'、'+dict['context']+'</div>'+
					'<div class="status status'+dict['AnswerStatus']+'">'+statusDesc+'</div></div>'+
				//'<div class="queContent">'+dict['context']+'</div>'+
				'<div class="timeInfo">提问者：'+dict['perName']+'&nbsp;&nbsp;'+dict.infoTime+
					'<div class="openBtn">回答<div><img src="../dependence/teach/images/angledown.svg"></div></div></div>'+
				'<div class="ansArea">'+ansHtml+'</div>'
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.id = 'cell-'+index
			newLi.innerHTML = html
			UL.appendChild(newLi)
		}
	}
	function saveTeachSub(){
		var index = this.id
		var input = $('#input-'+index)[0]
		if (input.value=="") {
			mui.toast('回答内容不能为空哦')
			return
		}
		var cellDom = $('#cell-'+index)[0]
		var dict = sourceData[index]
		var flag = this.getAttribute('data-id')
		var parr = dict['rw']+'「'+input.value+'「'+flag;
		var pdict = {'parr':parr,'activeName':4}
		//console.log(JSON.stringify(pdict))
		var paramDict = {'method':'web.INMTeaComm:SaveTeachSub','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1') {
				mui.toast(result.msg)
				return
			}
			//refreshMainPage()
			var statusDom = cellDom.querySelector('.status')
			if (flag == 'Y') {
				cellDom.querySelector('.ansArea').innerHTML = '【回复】'+input.value
				mui.toast('提交成功')
				statusDom.setAttribute('class','status statusY')
				statusDom.innerHTML = '已提交'
			}else{
				mui.toast('保存成功')
				statusDom.innerHTML = '已保存'
				statusDom.setAttribute('class','status statusN')
			}
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	function openBtnTapped(){
		var parentNode = this.parentNode.parentNode;
		if (parentNode.getAttribute('class').indexOf('dhc-active') != -1) {
			parentNode.classList.remove('dhc-active')
		}else{
			parentNode.classList.add('dhc-active')
		}
	}
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		preData = self.sourceData
		
		mui.previewImage({
			zoom: true,
		});
		mui('#wrapper').on('tap','.openBtn',openBtnTapped)
		mui('#wrapper').on('tap','.mui-btn-primary',saveTeachSub) 
		
		mui("body").on("tap", ".searchArea .mui-icon-clear", function() {
			if (document.getElementById('search').value != "") {
				document.getElementById('search').value = ""
			}
			refreshMainPage()
		});
		var search = document.getElementById('search');
		search.addEventListener('input',function(){
			refreshMainPage()
		});
		
		var scroll = document.getElementById('scroll');
		pullObj = $(scroll).pullToRefresh({
			up: {
				callback: function() {
					getAskData()
				}
			}
		});
		getAskData()
	}
	function refreshMainPage(){
		page = 0;
		sourceData = []
		if (pullObj) {
			pullObj.refresh(true)
		}
		getAskData();
		$('#wrapper').scroll().scrollTo(0,0,100);
	}
	function getFocusLocation(dom){
		if (window.getSelection) {//ie11 10 9 ff safari
		    dom.focus(); //解决ff不获取焦点无法定位问题
		    var range = window.getSelection();//创建range
		    range.selectAllChildren(dom);//range 选择obj下所有子内容
		    range.collapseToEnd();//光标移至最后
		}else if (document.selection) {//ie10 9 8 7 6 5
		    var range = document.selection.createRange();//创建选择对象
		    //var range = document.body.createTextRange();
		    range.moveToElementText(dom);//range定位到obj
		    range.collapse(false);//光标移至最后
		    range.select();
		}
	}
})(mui);